package com.hl.payServer.service.impl;

import com.hl.payServer.mapper.PayProjectMapper;
import com.hl.payServer.model.PayProject;
import com.hl.payServer.model.ResultModel;
import com.hl.payServer.service.PayProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;


@Service
public class PayProjectServiceImpl implements PayProjectService {

    @Autowired
    private PayProjectMapper payProjectMapper;

    @Override
    public List<PayProject> queryList(PayProject entity) {
        Example example = new Example(PayProject.class);
        Example.Criteria criteria1 = example.createCriteria();
        if (StringUtil.isNotEmpty(entity.getProjectCode())) {
            criteria1.andEqualTo("projectCode", entity.getProjectCode());
        }
        if (StringUtil.isNotEmpty(entity.getProjectName())) {
            criteria1.andEqualTo("projectName", entity.getProjectName());
        }
        return payProjectMapper.selectByExample(example);
    }

    @Override
    public ResultModel save(PayProject entity) {
        Example example = new Example(PayProject.class);
        Example.Criteria criteria1 = example.createCriteria();
        criteria1.orEqualTo("projectCode", entity.getProjectCode());
        criteria1.orEqualTo("projectName", entity.getProjectName());
        List<PayProject> list = payProjectMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return new ResultModel(false,"名称或编码重复,本次操作失败!");
        }
        payProjectMapper.insertSelective(entity);
        return ResultModel.ok(entity);
    }

    @Override
    public ResultModel update(PayProject entity) {
        Example example = new Example(PayProject.class);
        Example.Criteria criteria1 = example.createCriteria();
        criteria1.orEqualTo("projectCode", entity.getProjectCode());
        criteria1.orEqualTo("projectName", entity.getProjectName());
        Example.Criteria criteria2 = example.createCriteria();
        criteria2.andNotEqualTo("id", entity.getId());
        example.and(criteria2);
        List<PayProject> list = payProjectMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return new ResultModel(false,"名称或编码重复,本次操作失败!");
        }
        payProjectMapper.updateByPrimaryKeySelective(entity);
        return ResultModel.ok(entity);
    }

    @Override
    public ResultModel delete(String id) {
        if (id != null) {
            payProjectMapper.deleteByPrimaryKey(id);
        }
        return ResultModel.ok();
    }


}
